package cn.kgc.springbootvuefaq.controller;

import cn.kgc.springbootvuefaq.ResultEntity;
import cn.kgc.springbootvuefaq.pojo.Classes;
import cn.kgc.springbootvuefaq.pojo.ClassesFaqs;
import cn.kgc.springbootvuefaq.pojo.Faqs;
import cn.kgc.springbootvuefaq.service.ClassesService;
import cn.kgc.springbootvuefaq.service.FaqsService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

@RestController
public class FaqController {
    @Resource
    FaqsService service1;
    @Resource
    ClassesService service2;

    /*显示分类*/
    @RequestMapping("/getSelectClasses")
    public ResultEntity<List<Classes>> getSelectDistrict(){
        List<Classes> list=service2.queryAll();
        return ResultEntity.successWithoutData(list);
    }
    /*显示问题*/
    @RequestMapping("/getSelectFaqsByclassid")
    public ResultEntity<PageInfo<ClassesFaqs>> getSelectFaqsByclassid(@RequestParam(value = "classid",required = false,defaultValue = "0") Integer classid,
                                                                   @RequestParam(value = "pageNum",required = false,defaultValue = "1") Integer pageNum,
                                                                   @RequestParam(value = "pageSize",required = false,defaultValue = "5") Integer pageSize){
        PageInfo<ClassesFaqs> pageInfo=service1.queryAll(classid, pageNum, pageSize);
        return ResultEntity.successWithoutData(pageInfo);
    }
    /*添加*/
    @RequestMapping("/savefaq")
    public ResultEntity<Integer> savefaq(@RequestBody Faqs faqs){
        faqs.setCreatedate(new Date());
            Integer result=service1.Add(faqs);
            return ResultEntity.successWithoutData(result);
    }

    /*根据Id查询*/
    @RequestMapping("/SelectByid")
    public ResultEntity<ClassesFaqs> getSelectByid(Integer id) throws ParseException {
        ClassesFaqs classesFaqs=service1.queryByid(id);
        return ResultEntity.successWithoutData(classesFaqs);
    }

    /*根据title查询*/
    @RequestMapping("/SelectBytitle")
    public ResultEntity<Faqs> getSelectByfaqs( String title){
        List<Faqs> faqs=service1.queryByName(title);
        if(faqs.size()!=0){
            return ResultEntity.successWithoutData(faqs.get(0));
        }else{
            return ResultEntity.successWithoutData(null);
        }

    }
}
